<?php


namespace app\admin\service;


use app\admin\model\Config as ConfigModel;
use app\admin\validate\Config;
use app\common\service\BaseAdminService;
use app\traits\Result;
use think\exception\ValidateException;

class ConfigService extends BaseAdminService
{


    protected static $sceneLabel = [
        'add' => '配置添加',
        'edit' => '配置编辑'
    ];

    /**
     * 添加/编辑配置
     * @param $data
     * @param string $scene
     * @return array|string
     */
    public static function storeConfig($data,$scene='add'){
        try {
            validate(Config::class)->scene($scene)->check($data);
        } catch (ValidateException $e) {
            return self::error($e->getError(),null, ['token'=>request()->buildToken()]);
        }
        $model = new ConfigModel();

        //编辑
        if ($scene !='add') {
            $result = $model->exists(true)->update($data);
        } else {
            $result = $model->create($data);
        }
        return  self::getSceneMessage($result,$scene);
    }

}
